﻿using System.Collections.Generic;
using Models;
using Views;

namespace Presenters
{
    /// <summary>
    /// Presenter for the Remove Databases window
    /// </summary>
    public class RemoveDatabasesPresenter : PresenterBase<IRemoveDatabasesView>
    {
        /// <summary>
        /// Initialise the Remove Databases presenter
        /// </summary>
        /// <param name="view">A view that impliments IRemoveDatabasesView</param>
        /// <param name="parentQueryTabView">The Query Tab View that we were lauched from</param>
        public RemoveDatabasesPresenter(IRemoveDatabasesView view, IQueryView parentQueryTabView)
            : base(view)
        {
            m_ParentQueryTabView = parentQueryTabView;

            if ((parentQueryTabView != null) && (parentQueryTabView.DatabaseList != null))
            {
                View.LoadedDatabases = new List<Database>();
                foreach (Database database in parentQueryTabView.DatabaseList)
                {
                    Database clonedDatabase = new Database(database);
                    clonedDatabase.Selected = false;
                    View.LoadedDatabases.Add(new Database(clonedDatabase));
                }
            }
        }

        /// <summary>
        /// Remove the selected databases from the list of loaded databases
        /// </summary>
        public void RemoveDatabasesFromList()
        {
            if (View.LoadedDatabases != null)
            {
                View.LoadedDatabases.RemoveAll(db => db.Selected);

                m_ParentQueryTabView.DatabaseList = View.LoadedDatabases;
            }
        }

        /// <summary>
        /// Select all of the available databases
        /// </summary>
        public void SelectAll()
        {
            if (View.LoadedDatabases != null)
            {
                foreach (Models.Database db in View.LoadedDatabases)
                {
                    db.Selected = true;
                }
            }
        }

        /// <summary>
        /// Select none of the available databases
        /// </summary>
        public void SelectNone()
        {
            if (View.LoadedDatabases != null)
            {
                foreach (Models.Database db in View.LoadedDatabases)
                {
                    db.Selected = false;
                }
            }
        }

        /// <summary>
        /// The query tab view that the remove database process was launched from
        /// </summary>
        private readonly IQueryView m_ParentQueryTabView;
    }
}
